package com.mlnx.web.data.controller;


import com.alibaba.fastjson.JSONObject;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.form.PageForm;
import com.mlnx.data.entity.BpInfo;
import com.mlnx.data.pojo.form.BpInfoForm;
import com.mlnx.data.service.BpDataService;
import com.mlnx.device.entity.Device;
import com.mlnx.device.service.DeviceService;
import com.mlnx.external.push.tp.ReqMsg;
import com.mlnx.stream.service.DeviceBroadcastService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api(value = "血压数据接口", tags = {"血压数据接口"})
@RestController
@RequestMapping("mlnx_data/bp")
public class BpController extends BaseController {
    @Autowired
    private BpDataService bpDataService;

    @Autowired
    private DeviceBroadcastService deviceBroadcastService;

    @Autowired
    private DeviceService deviceService;


    @ApiOperation("获取血压数据")
    @GetMapping(value = "/bp")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "patientId", value = "患者ID", required = true)
    })
    public Response getBps(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime,
                           @RequestParam("patientId") Integer patientId) {
        return result(bpDataService.getBps(patientId, startTime, endTime));
    }

    @ApiOperation("更新血压数据")
    @PutMapping(value = "")
    public Response updateBps(@Valid BpInfo bpInfo) {
        bpDataService.updateBp(bpInfo);
        return result();
    }

    @ApiOperation("删除血压数据")
    @DeleteMapping(value = "")
    public Response deleteBp(@RequestBody List<BpInfo> bpInfos) {
        bpDataService.deleteBps(bpInfos);

        return result();
    }

    @ApiOperation("分页获取所有血压数据")
    @GetMapping(value = "")
    public ResponseData getAllBps(@Valid PageForm pageForm, @Valid BpInfoForm bpInfoForm) {

        return result(bpDataService.getAllBps(pageForm, bpInfoForm));
    }

    @ApiOperation("新增血压数据")
    @PostMapping(value = "")
    public Response addBp(@Valid BpInfo bpInfo) {
        Device device = deviceService.getDevice(bpInfo.getDeviceId());
        if(device!=null){
            if(device.getUserId()!=null){
                bpInfo.setUserId(device.getUserId());
            }
        }
        deviceBroadcastService.castBpInfo(bpInfo);
        return result();
    }

    @ApiOperation("获得推送的血压数据")
    @PostMapping(value = "data")
    public Response getPushBp(@RequestBody ReqMsg reqMsg) {
        System.out.println(reqMsg.toString());
        System.out.println(JSONObject.toJSONString(reqMsg));
        return result();
    }
}
